//
//  GMCommentCell.m
//  gentleman
//
//  Created by 范月成 on 2017/12/8.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMCommentCell.h"
#import "VendorHeader.h"
#import "ClassesHeader.h"
#import "GMGallery.h"

@interface GMCommentCell()

@property (nonatomic, strong) YYLabel *repostTimeLabel;
@property (nonatomic, strong) YYLabel *reporterLabel;
@property (nonatomic, strong) YYLabel *scoreLabel;
@property (nonatomic, strong) YYLabel *contentLabel;

@end

@implementation GMCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.reporterLabel];
        [self.contentView addSubview:self.scoreLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.repostTimeLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.reporterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(6);
        make.left.equalTo(self.contentView).offset(12);
        make.height.mas_equalTo(20);
        make.width.mas_lessThanOrEqualTo(kScreenWidth-124);
    }];
    
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(6);
        make.right.equalTo(self.contentView).offset(-12);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(20);
    }];
    
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.reporterLabel.mas_bottom).offset(6);
        make.left.equalTo(self.contentView).offset(12);
        make.right.equalTo(self.contentView).offset(-12);
        make.height.mas_equalTo([self.contentLabel.text heightForFont:kSystemFont(13) width:kScreenWidth-24]);
    }];
    
    [self.repostTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(6);
        make.left.equalTo(self.contentView).offset(12);
        make.height.mas_equalTo(20);
    }];
}

- (void)setComment:(GMGalleryComment *)comment {
    _comment = comment;
    self.reporterLabel.text = comment.reporter;
    self.scoreLabel.text = comment.score;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithData:[comment.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    attri.yy_font = kSystemFont(13);
    attri.yy_color = [UIColor themeBlack];

    self.contentLabel.attributedText = attri;
    self.repostTimeLabel.text = comment.repostTime;
}

- (CGSize)sizeThatFits:(CGSize)size {
    return CGSizeMake(size.width, 6+20+6+[self.contentLabel.text heightForFont:kSystemFont(13) width:kScreenWidth-24]+6+20+6);
}

#pragma mark - get
- (YYLabel *)reporterLabel {
    if (!_reporterLabel) {
        _reporterLabel = [GMUIHelper labelWithFont:kSystemBoldFont(13) textColor:[UIColor themeBlue]];
    }
    return _reporterLabel;
}

- (YYLabel *)scoreLabel {
    if (!_scoreLabel) {
        _scoreLabel = [GMUIHelper labelWithFont:kSystemFont(13) textColor:[UIColor themeGray]];
        _scoreLabel.textAlignment = NSTextAlignmentRight;
    }
    return _scoreLabel;
}

- (YYLabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [GMUIHelper labelWithFont:kSystemFont(13) textColor:[UIColor themeBlack]];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (YYLabel *)repostTimeLabel {
    if (!_repostTimeLabel) {
        _repostTimeLabel = [GMUIHelper labelWithFont:kSystemFont(13) textColor:[UIColor themeGray]];
    }
    return _repostTimeLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
